package org.gdufe.logs.controller;

import org.gdufe.logs.service.LogService;
import org.gdufe.pojos.dto.InsertLogDto;
import org.gdufe.pojos.dto.PageParams;
import org.gdufe.pojos.vo.PageResponseResult;
import org.gdufe.pojos.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

//借阅记录
@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private LogService logService;
    //查看所有借阅记录
    @GetMapping("/querry")
    @PreAuthorize("hasAuthority('super')")
    public PageResponseResult querry( PageParams pageParams, @RequestParam(required = false) String username,@RequestParam(required = false)Integer bId){
        return logService.querry(pageParams,username,bId);
    }

    //添加借阅记录
    @PostMapping("/insert")
    @PreAuthorize("hasAuthority('super')")
    public R insertLog(InsertLogDto dto){
        return logService.insertLog(dto);
    }

    //挂失
    @GetMapping("/loss/{id}")
    public R loss(@PathVariable Integer id){
        return logService.loss(id);
    }

    //查看当前用户借阅信息
    @GetMapping("/userquerry")
    public PageResponseResult userQuerry( PageParams pageParams,@RequestParam(required = false)Integer bId){
        Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principalObj.toString();
        return logService.querry(pageParams,username,bId);
    }
}
